package com.purchase.ml;

import com.purchase.ml.consts.CommonConsts;
import com.purchase.ml.consts.Constants;
import com.purchase.ml.entity.acc.SysUser;
import com.purchase.ml.entity.dealer.SysAddr;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.entity.sys.SysRoleUser;
import com.purchase.ml.enums.SysSeqNameEnum;
import com.purchase.ml.mapper.dealer.SysAddrMapper;
import com.purchase.ml.mapper.dealer.SysDealerMapper;
import com.purchase.ml.mapper.sys.SysRoleMapper;
import com.purchase.ml.mapper.sys.SysRoleUserMapper;
import com.purchase.ml.mapper.sys.SysUserMapper;
import com.purchase.ml.service.common.CommonSeqService;
import com.purchase.ml.utils.MD5;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author :zxm
 * @date :2021/3/31 11:12
 * @description :some description
 **/
@SpringBootTest
public class DealerDataInsert {

    @Autowired
    private CommonSeqService commonSeqService;
    @Autowired
    private SysDealerMapper sysDealerMapper;
    @Autowired
    private SysAddrMapper sysAddrMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Rollback(false)
    @Transactional
    @Test
    public void insertDealer(){
        // 插入20条主经销商
        for (int i = 1;i <= 20;i++){
            Long shopAddrId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_ADDR_GENERATE.getCode());
            Long billingAddrId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_ADDR_GENERATE.getCode());
            SysAddr shopAddr = new SysAddr();
            shopAddr.setAddrId(shopAddrId);
            shopAddr.setName("zhang"+i);
            shopAddr.setPhoneNumber("13123539009");
            shopAddr.setPostCode("432900");
            shopAddr.setCity("武汉市");
            shopAddr.setRegion("Negeri Sarawak");
            shopAddr.setCountry("Malaysia");
            shopAddr.setStreetAddress("马房山街道");
            shopAddr.setType(0);
            shopAddr.setRegionalAddress("武汉市洪山区");
            sysAddrMapper.insert(shopAddr);
            SysAddr billAddr = new SysAddr();
            billAddr.setAddrId(billingAddrId);
            billAddr.setName("li"+i);
            billAddr.setPhoneNumber("15099008097");
            billAddr.setPostCode("543090");
            billAddr.setCity("武汉市");
            billAddr.setRegion("Negeri Sarawak");
            billAddr.setCountry("Malaysia");
            billAddr.setStreetAddress("佛祖岭街道");
            billAddr.setType(1);
            billAddr.setRegionalAddress("武汉市江夏区");
            sysAddrMapper.insert(billAddr);

            SysDealer masterDealer = new SysDealer();
            masterDealer.setCreateAt(new Date());
            masterDealer.setSuperior(110725916L);
            masterDealer.setDealerName("主经销商代号"+i);
            masterDealer.setLastName("LastName代号"+i);
            masterDealer.setSyarikatCode("10000000000233330553");
            masterDealer.setRegistCode("EW80X1");
            masterDealer.setInviteCode("999"+i);
            masterDealer.setPhoneNumber("13212360897");
            masterDealer.setLogoUrl("/images/logo/"+ i +".jpg");
            masterDealer.setTypeCompany(1);
            masterDealer.setTypeBusiness("零售业");
            masterDealer.setEmailAddress("76123123@qq.com");
            Long mDealerId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_DEALER_GENERATE.getCode());
            masterDealer.setDealerId(mDealerId);
            masterDealer.setShopAddrId(shopAddrId);
            masterDealer.setBillingAddrId(billingAddrId);
            masterDealer.setDealerType(CommonConsts.ROLETYPE_MASTER);
            masterDealer.setState(Constants.DEALER_STATE_ENABLE);
            masterDealer.setRegion(1);
            masterDealer.setCertificateUrl("/images/certificate/"+ i +".jpg");
            masterDealer.setCompanyType(0);
            masterDealer.setNeedPay(0);
            sysDealerMapper.insert(masterDealer);

            SysUser sysUser = new SysUser();
            sysUser.setAcc("wdfMaster"+i+"@163.com");
            sysUser.setSalt("aaa");
            sysUser.setPwd(MD5.md5(CommonConsts.DEFAULT_PWD, sysUser.getSalt()));
            sysUser.setDealerId(mDealerId);
            sysUser.setRoleType(CommonConsts.ROLETYPE_MASTER);
            sysUser.setHeadUrl("/images/head/"+i+".jpg");
            sysUser.setName(masterDealer.getDealerName());
            sysUser.setEmail(sysUser.getAcc());
            sysUser.setState(0);
            sysUser.setIsAdmin(0);
            sysUser.setCreateAt(new Date());
            sysUser.setTelNumber("13212608032");
            sysUserMapper.insert(sysUser);
            SysRoleUser mRoleUser = new SysRoleUser();
            mRoleUser.setUserId(sysUser.getId());
            mRoleUser.setRoleId(Long.valueOf(CommonConsts.ROLETYPE_MASTER));
            sysRoleUserMapper.insert(mRoleUser);

            for (int j = 1; j <= 20; j++) {
                Long dShopAddrId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_ADDR_GENERATE.getCode());
                Long dBillingAddrId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_ADDR_GENERATE.getCode());
                SysAddr dShopAddr = new SysAddr();
                dShopAddr.setAddrId(dShopAddrId);
                dShopAddr.setName("wang"+i);
                dShopAddr.setPhoneNumber("14265780099");
                dShopAddr.setPostCode("432900");
                dShopAddr.setCity("长沙市");
                dShopAddr.setRegion("Negeri Sarawak");
                dShopAddr.setCountry("Malaysia");
                dShopAddr.setStreetAddress("灯笼里街道");
                dShopAddr.setType(0);
                dShopAddr.setRegionalAddress("长沙市");
                sysAddrMapper.insert(dShopAddr);
                SysAddr dBillAddr = new SysAddr();
                dBillAddr.setAddrId(dBillingAddrId);
                dBillAddr.setName("zhao"+i);
                dBillAddr.setPhoneNumber("13145068799");
                dBillAddr.setPostCode("543090");
                dBillAddr.setCity("鄂州市");
                dBillAddr.setRegion("Negeri Sarawak");
                dBillAddr.setCountry("Malaysia");
                dBillAddr.setStreetAddress("乡村故土街道");
                dBillAddr.setType(1);
                dBillAddr.setRegionalAddress("鄂州市");
                sysAddrMapper.insert(dBillAddr);

                // 每条主经销商插入20条经销商数据
                SysDealer sysDealer = new SysDealer();
                sysDealer.setCreateAt(new Date());
                sysDealer.setSuperior(masterDealer.getDealerId());
                sysDealer.setDealerName("经销商代号"+i+"-"+j);
                sysDealer.setLastName("经销商LastName"+i+"-"+j);
                sysDealer.setSyarikatCode("1000000000654331");
                sysDealer.setRegistCode("999"+i);
                sysDealer.setPhoneNumber("13212360897");
                sysDealer.setLogoUrl("/images/logo/"+ i +".jpg");
                sysDealer.setTypeCompany(1);
                sysDealer.setTypeBusiness("计算机");
                sysDealer.setEmailAddress("885886651@qq.com");
                Long dealerId = commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_DEALER_GENERATE.getCode());
                sysDealer.setDealerId(dealerId);
                sysDealer.setShopAddrId(dShopAddrId);
                sysDealer.setBillingAddrId(dBillingAddrId);
                sysDealer.setDealerType(CommonConsts.ROLETYPE_DEALER);
                sysDealer.setState(Constants.DEALER_STATE_ENABLE);
                sysDealer.setRegion(1);
                sysDealer.setCertificateUrl("/images/certificate/"+ i +".jpg");
                sysDealer.setCompanyType(0);
                sysDealer.setNeedPay(0);
                sysDealerMapper.insert(sysDealer);

                SysUser dealerUser = new SysUser();
                dealerUser.setAcc("wdfDealer"+i+"-"+j+"@163.com");
                dealerUser.setSalt("aaa");
                dealerUser.setPwd(MD5.md5(CommonConsts.DEFAULT_PWD, sysUser.getSalt()));
                dealerUser.setDealerId(dealerId);
                dealerUser.setRoleType(CommonConsts.ROLETYPE_DEALER);
                dealerUser.setHeadUrl("/images/head/"+i+".jpg");
                dealerUser.setName(sysDealer.getDealerName());
                dealerUser.setEmail(dealerUser.getAcc());
                dealerUser.setState(0);
                dealerUser.setIsAdmin(0);
                dealerUser.setCreateAt(new Date());
                dealerUser.setTelNumber("13567878809");
                sysUserMapper.insert(dealerUser);

                SysRoleUser sysRoleUser = new SysRoleUser();
                sysRoleUser.setUserId(dealerUser.getId());
                sysRoleUser.setRoleId(Long.valueOf(CommonConsts.ROLETYPE_DEALER));
                sysRoleUserMapper.insert(sysRoleUser);
            }
        }
    }

}
